package XComponent
{
	import XComponent.config.EnterGameSkinLAFXML;
	import XComponent.config.PreSkinLAFXML;
	import XComponent.show.XFrame;
	import XComponent.show.XGrid;
	import XComponent.show.XGridBox;
	import XComponent.show.XListTree;
	import XComponent.show.XLoader;
	import XComponent.show.XPopWnd;
	import XComponent.show.XSimpleButton;
	
	import br.com.stimuli.loading.BulkLoader;
	import br.com.stimuli.loading.BulkProgressEvent;
	
	import fl.containers.ScrollPane;
	import fl.containers.UILoader;
	import fl.controls.Button;
	import fl.controls.CheckBox;
	import fl.controls.ColorPicker;
	import fl.controls.ComboBox;
	import fl.controls.DataGrid;
	import fl.controls.Label;
	import fl.controls.List;
	import fl.controls.NumericStepper;
	import fl.controls.ProgressBar;
	import fl.controls.RadioButton;
	import fl.controls.Slider;
	import fl.controls.TextArea;
	import fl.controls.TextInput;
	import fl.controls.TileList;
	import fl.controls.UIScrollBar;
	
	import flash.display.Loader;
	import flash.display.SimpleButton;
	import flash.events.Event;
	import flash.events.ProgressEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.system.ApplicationDomain;
	import flash.system.LoaderContext;

	public class XComponents
	{
		static public var SKINURL_PRELOADING : String = "assets/mv/PreSkinLAF.swf";
		static public var SKINURL_ENTERGAME  : String = "assets/mv/SkinLAF.swf";
		static public var SKINCONFIGURL_PRELOADING : String = "assets/xml/PreSkinLAF.xml";
		static public var SKINCONFIGURL_ENTERGAME : String = "assets/xml/SkinLAF.xml";
		
		static private var m_loadSkinLAFProgressFun : Function;
		static private var m_curLoaded : Number;
		static private var m_configTotalLoaded : Number=0;
		static private var m_skinLAFTotalLoaded : Number=0;
		
		static private var _skinDomain : ApplicationDomain;
		static private var _callBack : Function;
		static private var m_brLoadSource : BulkLoader;
		
		private static var m_isPreLoad:Boolean;
		private static var m_isEnterGame:Boolean;
		
		static private var PreSkinLAFConfig : PreSkinLAFXML = new PreSkinLAFXML;
		static private var EnterGameSkinLAFConfig : EnterGameSkinLAFXML = new EnterGameSkinLAFXML;

		static private const m_xComponent : Object = 
		{
			Button:				Button,
			CheckBox : 			CheckBox,
			ColorPicker : 		ColorPicker,
			ComboBox : 			ComboBox,
			DataGrid : 			DataGrid,
			Label : 			Label,
			List : 				List,
			NumericStepper:		NumericStepper,
			ProgressBar: 		ProgressBar,
			RadioButton : 		RadioButton,
			ScrollPane : 		ScrollPane,
			Slider : 			Slider,
			TextArea : 			TextArea,
			TextInput : 		TextInput,
			TileList : 			TileList,
			UILoader : 			UILoader,
			UIScrollBar : 		UIScrollBar,
			SimpleButton : 		SimpleButton,
			XGrid : 			XGrid,
			XGridBox :  		XGridBox,		
			XListTree : 		XListTree,
			XSimpleButton :		XSimpleButton,
			XLoader : 			XLoader,
			XFrame : 			XFrame,
			XPopWnd : 			XPopWnd
		};		
		
		public function XComponents()
		{
		}
		
		static public function  component(componentName:String) : *
		{
			return m_xComponent[componentName];
		}

		static public function addComponent(id:String,value:Object):void
		{
			m_xComponent[id] = value;
		}
		
		/**
		 * 
		 * @param skinURL 	
		 * @param callBack
		 * 
		 */		
		static public function addSkinLAF(skinURL : String,id:String,priority:int=0) : void
		{
			if(!_skinDomain)
			{
				_skinDomain = new ApplicationDomain;
			}
			
			if(!m_brLoadSource)
			{
				m_brLoadSource = new BulkLoader("ComponentSkinLAF");
			}
			
			var loadContext : LoaderContext = new LoaderContext(false,_skinDomain);
			
			m_brLoadSource.add(skinURL,{id :id,priority : priority,context:loadContext });
		}
		
		static public function start(callBack : Function=null,progress:Function=null) : void
		{
			_callBack = callBack;
			m_loadSkinLAFProgressFun = progress;
		
			m_brLoadSource.addEventListener(BulkProgressEvent.PROGRESS,loadSkinLAFCompleteHandle);
			m_brLoadSource.addEventListener(BulkProgressEvent.COMPLETE,loadSkinLAFProgressHandle);
			
			m_brLoadSource.start();
		}
		
		static public function stop() : void
		{
			m_brLoadSource.removeEventListener(BulkProgressEvent.PROGRESS,loadSkinLAFCompleteHandle);
			m_brLoadSource.removeEventListener(BulkProgressEvent.COMPLETE,loadSkinLAFProgressHandle);
			
			_callBack = 				null
			m_loadSkinLAFProgressFun = 	null;
			m_isEnterGame = 			false;
			m_isPreLoad = 				false;
		}
		
		static private function loadSkinLAFCompleteHandle(evt : BulkProgressEvent) : void
		{
			if(!m_brLoadSource.isFinished) return;
			
			_callBack();
			
			if(m_isPreLoad)
			{
				PreSkinLAFConfig.prase();
			}
			
			if(m_isEnterGame)
			{
				EnterGameSkinLAFConfig.prase();
			}
			
			stop();
		}
		
		static private function loadSkinLAFProgressHandle(evt : BulkProgressEvent) : void
		{
			m_loadSkinLAFProgressFun(evt);
		}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////	
		static public function preSkinLAFLoad(callBack : Function=null,progress:Function=null) : void
		{
			addSkinLAF(SKINURL_PRELOADING,"PreSkinLAFSWF",0);
			addSkinLAF(SKINCONFIGURL_PRELOADING,"PreSkinLAFCFG",1);
			
			m_isPreLoad = true;
			
			start(callBack,progress);
		}
		
		static public function enterGameSkinLAFLoad(callBack : Function,progress:Function=null) : void
		{
			addSkinLAF(SKINURL_ENTERGAME,"EnterGameSkinLAFSWF",0);
			addSkinLAF(SKINCONFIGURL_ENTERGAME,"EnterGameSkinLAFCFG",1);
			
			m_isEnterGame = true;
			
			start(callBack,progress);
		}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////	
		static public function getSkin(skinName : String) : *
		{
			if(_skinDomain.hasDefinition(skinName))
			{
				return _skinDomain.getDefinition(skinName);
			}
			
			return null;
		}
		
		static public function getSkinLAFCongfigXML(id:String) : XML
		{
			return m_brLoadSource.getXML(id);
		}
		
		static public function getImgData(imgClassName : String) : Object
		{
			var data: Object;
			
			data = PreSkinLAFConfig.ImgData[imgClassName];
			
			if(!data)
			{
				data = EnterGameSkinLAFConfig.ImgData[imgClassName];
			}
			
			return data;
		}
		
		static public function getBtnData(imgClassName : String) : Object
		{
			var data: Object;
			
			data = PreSkinLAFConfig.BtnData[imgClassName];
			
			if(!data)
			{
				data = EnterGameSkinLAFConfig.BtnData[imgClassName];
			}
			
			return data;
		}
	}
}